﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using EShop.Common;
using EShop.BussinessObjects;
using MySql.Data.MySqlClient;

namespace EShop.DataAccess
{
    public class CDAProductShape
    {

        public static List<ProductShape> Select(ProductShape productShape)
        {
            MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            con.Open();


            string _cmdText =
                 "SELECT ps.* " +
                 "FROM vw_productshape ps " +
                 "WHERE ((ps.ID = " + productShape.ID.ToString() + ") OR (" + productShape.ID.ToString() + " = " + ((int)cnstDatabase.DBOperationResult.ValueIsNotEffective).ToString() + "))" +
                 "ORDER BY ps.ID";
            
            MySqlCommand cmd = new MySqlCommand(_cmdText, con);
            MySqlDataAdapter dataAdapter = new MySqlDataAdapter(_cmdText, con);
            DataTable tbl = new DataTable();
            dataAdapter.Fill(tbl);

            List<ProductShape> _productShapList = new List<ProductShape>();

            foreach (DataRow row in tbl.Rows)
            {
                ProductShape _productShape = new ProductShape();
                _productShape.ID = (row["ID"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["ID"]));
                
                _productShape.Product = new Product();
                _productShape.Product.ID = (row["ProductID"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["ProductID"]));
                
                _productShape.Shape = new Shape();
                _productShape.Shape.ID = (row["ShapeID"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["ShapeID"]));
                _productShape.Shape.Name = (row["ShapeName"].ToString() == "" ? null : row["ShapeName"].ToString());
                
                _productShape.Price = (row["Price"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToDecimal(row["Price"]));

                _productShapList.Add(_productShape);
            }

            con.Close();
            return _productShapList;
        }
    }
}